package com.weacar.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.gcssloop.widget.RCImageView;
import com.weacar.R;
import com.weacar.activity.LoginActivity;
import com.weacar.activity.MainActivity;
import com.weacar.activity.SettingActivity;
import com.weacar.bean.LoginBean;
import com.weacar.config.Constans;
import com.weacar.utils.SharedPreferenceUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MineFragment extends Fragment {


    private Unbinder unbinder;

    @BindView(R.id.rl_login)
    RelativeLayout rl_login;
    @BindView(R.id.cl_login)
    ConstraintLayout cl_login;
    @BindView(R.id.tv_nickname)
    TextView tv_nickname;
    @BindView(R.id.tv_balance)
    TextView tv_balance;
    @BindView(R.id.tv_deposit)
    TextView tv_deposit;
    @BindView(R.id.rc_image_header)
    RCImageView rc_image_header;
    @BindView(R.id.iv_level)
    ImageView iv_level;

    MainActivity mainActivity;


    @OnClick({R.id.tv_login, R.id.iv_setting, R.id.rc_image_header})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.tv_login:
                startActivity(new Intent(getActivity(), LoginActivity.class));
                break;
            case R.id.iv_setting:
            case R.id.rc_image_header:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
        }
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_mine, null);
        unbinder = ButterKnife.bind(this, v);
        mainActivity = (MainActivity) getActivity();
        initViews();
        return v;
    }


    private void initViews() {
        LoginBean loginBean = mainActivity.loginBean;
        if (loginBean != null) {
            LogUtils.d("header_img = " + loginBean.getMsg().getHeadimgurl());
            rl_login.setVisibility(View.GONE);
            cl_login.setVisibility(View.VISIBLE);
            if (!loginBean.getMsg().getHeadimgurl().equals("")) {
                Glide.with(this).load(loginBean.getMsg().getHeadimgurl()).placeholder(R.mipmap.icon_header_default).into(rc_image_header);
            }

            tv_nickname.setText(loginBean.getMsg().getNickname());
            tv_balance.setText(loginBean.getMsg().getMoney());
            tv_deposit.setText(loginBean.getMsg().getEarnest_money());

            switch (loginBean.getMsg().getLevel()) {
                case 1:
                    iv_level.setImageResource(R.mipmap.vip_putong);
                    break;
                case 2:
                    iv_level.setImageResource(R.mipmap.vip_baijin);
                    break;
                case 3:
                    iv_level.setImageResource(R.mipmap.vip_huangjin);
                    break;
                case 4:
                    iv_level.setImageResource(R.mipmap.vip_heijin);
                    break;
                case 5:
                    iv_level.setImageResource(R.mipmap.vip_zuanshi);
                    break;
            }


        } else {
            rl_login.setVisibility(View.VISIBLE);
            cl_login.setVisibility(View.GONE);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        if (Constans.isRefreshPersonInfo) {
            Constans.isRefreshPersonInfo = false;
            LoginBean loginBean = SharedPreferenceUtil.getObject(getContext(), "loginBean");
            if (loginBean != null) {
                if (!loginBean.getMsg().getHeadimgurl().equals("")) {
                    Glide.with(this).load(loginBean.getMsg().getHeadimgurl()).into(rc_image_header);
                }
                tv_nickname.setText(loginBean.getMsg().getNickname());
                tv_balance.setText(loginBean.getMsg().getMoney());
                tv_deposit.setText(loginBean.getMsg().getEarnest_money());

                switch (loginBean.getMsg().getLevel()) {
                    case 1:
                        iv_level.setImageResource(R.mipmap.vip_putong);
                        break;
                    case 2:
                        iv_level.setImageResource(R.mipmap.vip_baijin);
                        break;
                    case 3:
                        iv_level.setImageResource(R.mipmap.vip_huangjin);
                        break;
                    case 4:
                        iv_level.setImageResource(R.mipmap.vip_heijin);
                        break;
                    case 5:
                        iv_level.setImageResource(R.mipmap.vip_zuanshi);
                        break;
                }
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
